home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / BIGNUMIN.H < prev    next >
C/C++ Source or Header  |  1989-09-20  |  6KB  |  155 lines

  1. /* -*-C-*-
  2.  
  3. $Header: bignumint.h,v 1.1 89/09/20 23:19:45 GMT cph Exp $
  4.  
  5. Copyright (c) 1989 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Internal Interface to Bignum Code */
  36.  
  37. #undef BIGNUM_ZERO_P
  38. #undef BIGNUM_NEGATIVE_P
  39.  
  40. /* The memory model is based on the following definitions, and on the
  41.    definition of the type `bignum_type'.  The only other special
  42.    definition is `CHAR_BIT', which is defined in the Ansi C header
  43.    file "limits.h". */
  44.  
  45. typedef long bignum_digit_type;
  46. typedef long bignum_length_type;
  47.  
  48. #ifdef MIT_SCHEME
  49.  
  50. /* BIGNUM_ALLOCATE allocates a (length + 1)-element array of
  51.    `bignum_digit_type'; deallocation is the responsibility of the
  52.    user (in Scheme, the garbage collector handles this). */
  53. #define BIGNUM_ALLOCATE(length)                        \
  54.   (allocate_non_marked_vector                        \
  55.    (TC_BIG_FIXNUM, (BIGNUM_LENGTH_TO_GC_LENGTH (length)), 1))
  56.  
  57. /* BIGNUM_TO_POINTER casts a bignum object to a digit array pointer. */
  58. #define BIGNUM_TO_POINTER(bignum)                    \
  59.   ((bignum_digit_type *) (VECTOR_LOC ((bignum), 0)))
  60.  
  61. /* BIGNUM_REDUCE_LENGTH allows the memory system to reclaim some
  62.    space when a bignum's length is reduced from its original value. */
  63. #define BIGNUM_REDUCE_LENGTH(target, source, length)            \
  64. {                                    \
  65.   SET_VECTOR_LENGTH ((source), (BIGNUM_LENGTH_TO_GC_LENGTH (length)));    \
  66.   (target) = (source);                            \
  67. }
  68.  
  69. #define BIGNUM_LENGTH_TO_GC_LENGTH(length)                \
  70.   (BYTES_TO_WORDS (((length) + 1) * (sizeof (bignum_digit_type))))
  71.  
  72. /* BIGNUM_DEALLOCATE is called when disposing of bignums which are
  73.    created as intermediate temporaries; Scheme doesn't need this. */
  74. #define BIGNUM_DEALLOCATE(bignum)
  75.  
  76. /* If BIGNUM_FORCE_NEW_RESULTS is defined, all bignum-valued operations
  77.    return freshly-allocated results.  This is useful for some kinds of
  78.    memory deallocation strategies. */
  79. /* #define BIGNUM_FORCE_NEW_RESULTS */
  80.  
  81. /* BIGNUM_EXCEPTION is invoked to handle assertion violations. */
  82. #define BIGNUM_EXCEPTION error_external_return
  83.  
  84. #else /* not MIT_SCHEME */
  85.  
  86. #define BIGNUM_ALLOCATE bignum_malloc
  87. #define BIGNUM_TO_POINTER(bignum) ((bignum_digit_type *) (bignum))
  88. #define BIGNUM_REDUCE_LENGTH(target, source, length)            \
  89.   (target) = (bignum_realloc ((source), (length)))
  90. #define BIGNUM_DEALLOCATE free
  91. #define BIGNUM_FORCE_NEW_RESULTS
  92. #define BIGNUM_EXCEPTION abort
  93. #define fast register
  94. extern void free ();
  95. extern void abort ();
  96.  
  97. #endif /* MIT_SCHEME */
  98.  
  99. #define BIGNUM_DIGIT_LENGTH (((sizeof (bignum_digit_type)) * CHAR_BIT) - 2)
  100. #define BIGNUM_HALF_DIGIT_LENGTH (BIGNUM_DIGIT_LENGTH / 2)
  101. #define BIGNUM_RADIX         (1 << BIGNUM_DIGIT_LENGTH)
  102. #define BIGNUM_RADIX_ROOT     (1 << BIGNUM_HALF_DIGIT_LENGTH)
  103. #define BIGNUM_DIGIT_MASK     (BIGNUM_RADIX - 1)
  104. #define BIGNUM_HALF_DIGIT_MASK     (BIGNUM_RADIX_ROOT - 1)
  105.  
  106. #define BIGNUM_START_PTR(bignum)                    \
  107.   ((BIGNUM_TO_POINTER (bignum)) + 1)
  108.  
  109. #define BIGNUM_SET_HEADER(bignum, length, negative_p)            \
  110.   (* (BIGNUM_TO_POINTER (bignum))) =                    \
  111.     ((length) | ((negative_p) ? BIGNUM_RADIX : 0))
  112.  
  113. #define BIGNUM_LENGTH(bignum)                        \
  114.   ((* (BIGNUM_TO_POINTER (bignum))) & BIGNUM_DIGIT_MASK)
  115.  
  116. #define BIGNUM_NEGATIVE_P(bignum)                    \
  117.   (((* (BIGNUM_TO_POINTER (bignum))) & BIGNUM_RADIX) != 0)
  118.  
  119. #define BIGNUM_ZERO_P(bignum)                        \
  120.   ((BIGNUM_LENGTH (bignum)) == 0)
  121.  
  122. #define BIGNUM_REF(bignum, index)                    \
  123.   (* ((BIGNUM_START_PTR (bignum)) + (index)))
  124.  
  125. #ifdef BIGNUM_FORCE_NEW_RESULTS
  126. #define BIGNUM_MAYBE_COPY bignum_copy
  127. #else
  128. #define BIGNUM_MAYBE_COPY(bignum) bignum
  129. #endif
  130.  
  131. /* These definitions are here to facilitate caching of the constants
  132.    0, 1, and -1. */
  133. #define BIGNUM_ZERO bignum_make_zero
  134. #define BIGNUM_ONE bignum_make_one
  135.  
  136. #define HD_LOW(digit) ((digit) & BIGNUM_HALF_DIGIT_MASK)
  137. #define HD_HIGH(digit) ((digit) >> BIGNUM_HALF_DIGIT_LENGTH)
  138. #define HD_CONS(high, low) (((high) << BIGNUM_HALF_DIGIT_LENGTH) | (low))
  139.  
  140. #define BIGNUM_BITS_TO_DIGITS(n)                    \
  141.   (((n) + (BIGNUM_DIGIT_LENGTH - 1)) / BIGNUM_DIGIT_LENGTH)
  142.  
  143. #define BIGNUM_DIGITS_FOR_LONG                        \
  144.   (BIGNUM_BITS_TO_DIGITS ((sizeof (long)) * CHAR_BIT))
  145.  
  146. #ifndef BIGNUM_DISABLE_ASSERTION_CHECKS
  147.  
  148. #define BIGNUM_ASSERT(expression)                    \
  149. {                                    \
  150.   if (! (expression))                            \
  151.     BIGNUM_EXCEPTION ();                        \
  152. }
  153.  
  154. #endif /* not BIGNUM_DISABLE_ASSERTION_CHECKS */
  155.